<?php
/*
# ------------------------------------------------------------------------
# ZTTools plugin for Joomla 1.6
# ------------------------------------------------------------------------
# Copyright(C) 2008-2011 www.zootemplate.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: ZooTemplate
# Websites: http://www.zootemplate.com
# ------------------------------------------------------------------------
*/

defined('_JEXEC') or die();
jimport('joomla.plugin.plugin');
jimport('joomla.application.module.helper');
jimport('joomla.html.parameter');

require_once(dirname(__FILE__) . DS . 'plg_ztools' . DS . 'define.php');
require_once(dirname(__FILE__) . DS . 'plg_ztools' . DS . 'common.php');

class plgSystemPlg_ZTools extends JPlugin
{
	
	var $plugin = null;
	var $plgParams = null;
	var $time = 0;
	
	function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);
		$this->plugin 		= &JPluginHelper::getPlugin('system', 'plg_ztools');
		$this->plgParams 	= new JParameter($this->plugin->params);
	}		
	
	function onContentPrepareForm($form, $data)
	{
		if($form->getName()=='com_menus.item')
		{
			JForm::addFormPath(JPATH_SITE . DS . ZT_4BACKEND_MENU_PARAMS);
			$form->loadFile('params', false);
		}
	}
	
	function onAfterRender()
	{
		global $app;		
		$document = &JFactory::getDocument();
		
		if($app->isSite() && $document->_type != 'feed')
		{
			ZTimport('plg_ztools.libs.ztgzip');
			$Gzip = new ZTGzip;
			
			if($app->getTemplate(true)->params->get('gzip_optimize_css', 0)) $Gzip->optimizecss();
			
			if($app->getTemplate(true)->params->get('gzip_optimize_js', 0)) $Gzip->optimizejs();
			
			if($app->getTemplate(true)->params->get('gzip_optimize_html', 1)) $Gzip->optimizehtml();
			
			$type	= JRequest::getVar('type');
			$action = JRequest::getVar('action');
			if($type == 'plugin' && $action == 'clearCache')
				$Gzip->clearCache();
		}
		else
		{
			$uri 	= str_replace(DS, "/", str_replace(JPATH_SITE, JURI::base(), dirname(__FILE__)));
			$uri 	= str_replace("/administrator/", "", $uri);
			$html 	= '<script language="javascript" type="text/javascript" 
			src="'.$uri.'/plg_ztools/4backend/menu/element/assets/js/bt_clear_cache.js"></script>';
			$buffer = JResponse::getBody ();
			$buffer = preg_replace('/<\/head>/', $html . "\n</head>", $buffer);
			JResponse::setBody($buffer);						
		}
	}
}
?>